
package com.roger.java;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;

public class NetworkJavaClass {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // getHostAddress("www.baidu.com");
        // getHostAddress("news.baidu.com");
        // getLocalHost();
        // checkIfPortOpened("www.baidu.com", 80 /*http*/);
        // checkIfPortOpened("www.baidu.com", 21 /*ftp*/);
        // checkIfPortOpened("www.baidu.com", 22 /*ssh*/);
        // checkIfPortOpened("www.baidu.com", 23 /*telnet*/);
        // checkIfPortOpened("webmail.marvell.com", 25 /* smtp */);
        // checkIfPortOpened("webmail.marvell.com", 110 /* pop3 */);
        // checkIfPortOpened("webmail.marvell.com", 143 /* imap */);
        extractWebContentBySocket("computing.dcu.ie", "/~humphrys/howtomailme.html", 80);
    }

    public static void getHostAddress(String host) {
        try {
            InetAddress address = InetAddress.getByName(host);
            System.out.println(host + ":" + address.getHostAddress());
        } catch (UnknownHostException e) {
            System.out.println(host + " is an unknow host");
            e.printStackTrace();
        }
    }

    public static void getLocalHost() {
        try {
            InetAddress address = InetAddress.getLocalHost();
            System.out.println("localhost is " + address.getHostAddress());
        } catch (UnknownHostException e) {
            System.out.println("localhost is unknown");
            e.printStackTrace();
        }
    }

    public static void checkIfPortOpened(String host, int port) {
        Socket socket = null;
        try {
             InetAddress address = InetAddress.getByName(host);
            socket = new Socket(host, port);
            System.out.println("There is a server(" + host + ", " + address.getHostAddress()
                    + ") running on port:" + port);
        } catch (UnknownHostException e) {
            System.out.println(host + " is unknown");
            e.printStackTrace();
        } catch (IOException e) {
            System.out.println(host + " doesn't open port: " + port);
            e.printStackTrace();
        } finally {
            if (socket != null) {
                try {
                    socket.close();
                } catch (IOException e) {
                    System.out.println("Socket closed failed");
                    e.printStackTrace();
                }
            }
        }
    }

    public static void extractWebContentBySocket(String host, String file, int port) {
        Socket socket = null;
        try {
            socket = new Socket(host, port);
            OutputStream os = socket.getOutputStream();
            PrintWriter pw = new PrintWriter(os);
            pw.print("GET " + file + " HTTP/1.0\r\n");
            pw.print("Accept: text/plain, text/html, text/*\r\n");
            pw.print("\r\n");
            pw.flush();

            InputStream in = socket.getInputStream();
            InputStreamReader inr = new InputStreamReader(in);
            BufferedReader br = new BufferedReader(inr);
            String line;
            while ((line = br.readLine()) != null)
            {
                System.out.println(line);
            }
        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (socket != null) {
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
